package com.bdc.whatwhat.fragments;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.activities.MainActivity;
import com.bdc.whatwhat.adapters.WhatWhatFluxAdapter;
import com.bdc.whatwhat.fragments.core.WWFragment;
import com.bdc.whatwhat.modele.MonFlux;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.task.FluxTask;
import com.bdc.whatwhat.task.MonFluxTask;
import com.bdc.whatwhat.views.Circle;
import com.bdc.whatwhat.views.SaisieWhatWhat;

import java.util.ArrayList;
import java.util.List;

public class FluxFragment extends WWFragment implements View.OnClickListener, FluxTask.FluxTaskDelegate, MonFluxTask.FluxTaskDelegate, WhatWhatFluxAdapter.WhatWhatFluxAdapterListener, FaireWhatWhatTask.FaireWhatWhatTaskDelegate {

    FaireWhatWhatTask faireWhatWhatTask;

    View view;

    ListView listView;
    WhatWhatFluxAdapter adapter;

    View circleButton;
    Circle circle;
    View saisieLayout;
    SaisieWhatWhat saisieWhatwhat;

    Whatter whatter;
    List<WhatWhat> whatWhatList = new ArrayList<WhatWhat>();

    SessionManager session;

    MonFluxTask fluxTask;

    int page = 1;
    boolean loadMore = true;
    boolean clear = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.fragment_flux, container, false);

        session = new SessionManager(getSherlockActivity());
        whatter = session.getProfil();

        charger();
        remplir();
        ajouterListeners();


        //fluxTask = new MonFluxTask(this);
        //fluxTask.execute(session.getToken());
        //listView.setOnItemClickListener(null);
        return view;
    }

    public static FluxFragment newInstance() {
        return new FluxFragment();
    }


    private void charger() {

        getSherlockActivity().getSupportActionBar().setTitle(getResources().getString(R.string.app_name));

        listView = (ListView) view.findViewById(android.R.id.list);

        circle = (Circle) view.findViewById(R.id.circle);
        circleButton = view.findViewById(R.id.circle_button);

        saisieLayout = view.findViewById(R.id.saisie_layout);
        saisieWhatwhat = (SaisieWhatWhat) view.findViewById(R.id.saisie_whatwhat);
    }

    private void remplir() {

        if (whatter == null)
            whatter = new Whatter();
        adapter = new WhatWhatFluxAdapter(getActivity(), whatter, whatWhatList, this);
        listView.setAdapter(adapter);
        circle.setViewToShow(saisieLayout);
        saisieWhatwhat.setCircle(circle);



        MonFlux monFlux = session.getMonFlux();
        if(monFlux != null)
            mettreAJour(monFlux.getFlux());

    }


    private void ajouterListeners() {
        circleButton.setOnClickListener(this);
        adapter.setListener(this);
    }


    @Override
    public boolean onBackPressed() {
        if (circle.onBackPressed())
            return true;
        else
            return super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        circle.open();
    }


    @Override
    public void onWhatWhatLoadMore() {

        if (loadMore) {
            FluxTask fluxTask1 = new FluxTask(this);
            fluxTask1.execute(page, session.getToken());
        }
    }

    //-----------------FluxTaskDelegate------------------


    @Override
    public void faire(WhatWhat whatWhat) {
        if (faireWhatWhatTask != null)
            faireWhatWhatTask.cancel(true);
        faireWhatWhatTask = new FaireWhatWhatTask(this, FaireWhatWhatTask.EXISTANT);
        faireWhatWhatTask.execute(whatWhat, session.getToken());
    }

    @Override
    public void afficherChargement(boolean afficher) {

    }

    @Override
    public void onFluxRecu(MonFlux monFlux) {

        session.setProfil(monFlux.getProfil());
        session.setMonFlux(monFlux);
        if (clear)
            mettreAJourFlux(monFlux.getFlux());
    }

    @Override
    public void onFluxRecu(List<WhatWhat> whatWhatList) {
        mettreAJourFlux(whatWhatList);
    }

    @Override
    public void onResultatVide() {
        loadMore = false;
    }

    @Override
    public void onErreurReseau() {
        loadMore = false;
    }


    //----------------FaireTaskDelegate------------------

    @Override
    public void onWhatWhatEnvoye(WhatWhat whatWhat) {

        try {
            whatter.setWhatWhat(whatWhat);
            ((MainActivity) getActivity()).notifyProfilChanged(whatter, null);

            page = 1;
            clear = true;
            fluxTask = new MonFluxTask(this);
            fluxTask.execute(session.getToken());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void mettreAJour(List<WhatWhat> whatWhatList) {

        whatter.setProfil(session.getProfil());
        adapter.notifyDataSetChanged();
        adapter.notifyDataSetInvalidated();

        page = 1;
        clear = true;

        if (whatWhatList != null) {
            mettreAJourFlux(whatWhatList);
        }else{
            FluxTask fluxTask1 = new FluxTask(this);
            fluxTask1.execute(1,session.getToken());
        }
    }


    private void mettreAJourFlux(List<WhatWhat> whatWhatList) {
        if (clear) {
            this.whatWhatList.clear();
            adapter.notifyDataSetInvalidated();
        }

        this.whatWhatList.addAll(whatWhatList);
        adapter.notifyDataSetChanged();


    }
}
